package com.codered;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BluetoothService extends Service {

	/**
	 * Variables
	 */
	
	// Constants
	protected static String TAG = "BluetoothService";
	protected static boolean DEBUG = true;
	protected static int ONGOING_NOTIFICATION = 411;
	
	
	
	
	
	
	
	/**
	 * Processes
	 */
	
	
	
	
	
	
	/**
	 * Overrides
	 */
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate() {
        if (DEBUG) Log.i(TAG, "-----onCreate fired-----");
        designateAsForeground();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    	startActivity(btIntent);
    	return START_STICKY;
    }
    
    @Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public void onDestroy() {
    	if (DEBUG) Log.i(TAG, "-----onDestroy fired-----");
    	removeForeground();
    }
    
    
    
    
    
    
    
    /**
     * Custom Functions
     */
    private void designateAsForeground() {
    	Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.notification_ticker_text),
    	        System.currentTimeMillis());
    	Intent notificationIntent = new Intent(this, CoderedActivity.class);
    	notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    	PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    	notification.setLatestEventInfo(this, getText(R.string.notification_title),
    	        getText(R.string.notification_message), pendingIntent);
    	startForeground(ONGOING_NOTIFICATION, notification);
    }
    
    private void removeForeground() {
    	stopForeground(true);
    }

}
